package com.yanfan.ba.modbus.test;


import com.serotonin.modbus4j.BatchRead;
import com.serotonin.modbus4j.ModbusMaster;
import com.serotonin.modbus4j.code.DataType;
import com.serotonin.modbus4j.exception.ErrorResponseException;
import com.serotonin.modbus4j.exception.ModbusInitException;
import com.serotonin.modbus4j.exception.ModbusTransportException;
import com.yanfan.ba.util.Modbus4jUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;

@Configuration
@EnableScheduling
public class GetModbusTCPDataTask {

//    @Value("${modbus.ip}")
    private String ip;

//    @Value("${modbus.port}")
    private Integer port;

   /* @Autowired
    private LocatorConfig locatorConfig;*/

    //@Scheduled(fixedRateString = "2000")1
    public void  getData() throws ModbusInitException, ModbusTransportException, ErrorResponseException {
        ModbusMaster master = Modbus4jUtils.getMaster(ip, port);
        BatchRead<Integer> batch = new BatchRead<>();
        for (int i = 0; i < 10; i++) {
            Number number = Modbus4jUtils.readHoldingRegister(master, 1, i, DataType.TWO_BYTE_INT_UNSIGNED);
            System.out.println(number);
        }
        System.out.println("结束==");


    }

    public static void main(String[] args) throws ModbusTransportException, ErrorResponseException, ModbusInitException {
        ModbusMaster master = Modbus4jUtils.getMaster("10.21.29.4", 502);
        BatchRead<Integer> batch = new BatchRead<>();
//        for (int i = 0; i < 10; i++) {
            Number number = Modbus4jUtils.readInputRegisters(master, 1, 55, DataType.TWO_BYTE_INT_UNSIGNED);
            System.out.println(number);
        //}
        System.out.println("结束==");
    }

}

